<template>
  <div class="feedback-container">
    <el-collapse >
      <el-collapse-item title="反馈说明" name="description"  style="min-height:200px;">
        <div class="description">
          <p style="color:brown">
            如果你对本系统或者政府部门的工作有自己更好的见解，请填写下您的反馈意见，反馈意见应该真实有效，我们将不断努力提升服务品质，感谢您的支持！
          </p>
        </div>
      </el-collapse-item>
      <el-collapse-item title="反馈申请" name="application">
        <div class="application">
          <el-form :model="form" label-width="100px" ref="form">
            <el-form-item label="反馈人">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="反馈内容">
              <el-input type="textarea" v-model="form.content"></el-input>
            </el-form-item>
            <el-form-item label="意见建议">
              <el-input type="textarea" v-model="form.suggestion"></el-input>
            </el-form-item>
            <el-form-item style="text-align: center;">
              <el-button type="primary" @click="submitForm">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: '',
        content: '',
        suggestion: ''
      }
    }
  },
  methods: {
    //转换时间方法
    getDate() {
      //date是传过来的时间戳，注意需为13位，10位需*1000
      //也可以不传,获取的就是当前时间
      var time = new Date();
      var year = time.getFullYear()  //年
      var month = ("0" + (time.getMonth() + 1)).slice(-2); //月
      var day = ("0" + (time.getDate())).slice(-2); //日
      var mydate = year + "-" + month + "-" + day;
      return mydate
    },
    submitForm() {
      if(!this.$store.state.userInfo){
        this.$message.error("请登录后再进行反馈！");
      }
      else{
      this.$refs.form.validate(valid => {
        if (valid) {
          // 提交表单数据到后端
          this.request.post('/suggest/explain', {
            name: this.form.name,
            content: this.form.content,
            suggestion: this.form.suggestion,
            user_id: this.$store.getters.getUser.id,
            time: this.getDate(),
          })
            .then(response => {
              this.$message.success("提交成功！");
              // 重置表单
              this.form = {};
            })
            .catch(error => {
              this.$message.error(error.response.data.message);
            });
        }
       })
      }
    },
    resetForm() {
      this.form = {}
    }
  }
}
</script>

<style>
.feedback-container {
  max-width: 800px;
  margin: 0 auto;
}

.description {
  padding: 20px;
  font-size: 16px;
  line-height: 1.5;
  background-color: antiquewhite;
}

.application {
  padding: 20px;
  background-color: cornsilk;
}
</style>
